package com.module.login.view

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.module.app.base.BaseActivity
import com.module.app.ext.loginFlow
import com.module.base.utils.StatusMyBarUtil
import com.module.base.utils.ToastUtils
import com.bisinuolan.app.base.bsnl_share.Utils.ClickUtils
import com.module.frame.ext.observe
import com.module.login.R
import com.module.login.bean.ILoginType
import com.module.login.databinding.LoginActivityLoginPhoneBinding
import com.module.login.model.LoginPhoneViewModel
import com.module.login.utils.UserConvertUtils
import com.module.login.utils.startLoginCode
import com.module.third.ext.qqLoginSharedFlow
import com.module.third.ext.wxLoginSharedFlow
import com.module.third.qq.QQUtils
import com.module.third.wx.WxUtils

class LoginPhoneActivity : BaseActivity<LoginPhoneViewModel, LoginActivityLoginPhoneBinding>() {

    val mLauncherService: com.module.app.arouter.service.ILauncherService by lazy {
        com.module.app.arouter.ArouterUtils.getLauncherService()
    }
    private var isSelect = false
    private var qqUtils: QQUtils? = null
    private var wxUtils: WxUtils? = null

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    override fun initView() {
        super.initView()
        StatusMyBarUtil.setPaddingSmart(getContext(), mDatabind.ivBack)
        mDatabind.ivCheckbox.setImageResource(if (isSelect) R.drawable.app_ic_checkbox_select else R.drawable.app_ic_checkbox)
    }

    override fun initListener() {
        super.initListener()
        //登录监听
        observe(loginFlow) {
            loginSuc()
        }
        //微信登录
        observe(wxLoginSharedFlow) {
            it?.let {
                mViewModel.login(
                    com.module.app.bean.IType.ILogin.WX,
                    it.openid,
                    UserConvertUtils.convertWx(it)
                )
            }
        }
        //qq登录
        observe(qqLoginSharedFlow) {
            it?.let {
                mViewModel.login(
                    com.module.app.bean.IType.ILogin.QQ,
                    it.openid,
                    UserConvertUtils.convertQQ(it)
                )

            }
        }
        mDatabind.etPhone.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                mDatabind.ivDel.visibility = if (s.isNotEmpty()) View.VISIBLE else View.GONE
                mDatabind.tvSend.isEnabled = s.length >= 11
            }
        })
    }

    fun onClickSend(view: View) {
        val phone = mDatabind.etPhone.text.toString()
        if (!mViewModel.isCheck(phone)) {
            return
        }
        if (!isSelect) {
            ToastUtils.showShort(R.string.login_checkbox_agreement_tips)
            return
        }
        startLoginCode(phone)
    }

    fun onClickBack(view: View) {
        finish()
    }

    fun onClickCheckBox(view: View) {
        isSelect = !isSelect
        mDatabind.ivCheckbox.setImageResource(if (isSelect) R.drawable.app_ic_checkbox_select else R.drawable.app_ic_checkbox)
    }

    fun onClickLaw(view: View) {
        com.module.app.web.WebViewActivity.startService(getContext())
    }

    fun onClickPrivate(view: View) {
        com.module.app.web.WebViewActivity.startPrivacy(getContext())
    }

    fun onClickDel(view: View) {
        mDatabind.etPhone.setText("")
    }

    fun onClickWX(view: View) {
        if (ClickUtils.isFastDoubleClick()) {
            return
        }
        if (!isSelect) {
            ToastUtils.showShort(R.string.login_checkbox_agreement_tips)
            return
        }
        getWxUtils()?.login()
    }

    fun onClickQQ(view: View) {
        if (ClickUtils.isFastDoubleClick()) {
            return
        }
        if (!isSelect) {
            ToastUtils.showShort(R.string.login_checkbox_agreement_tips)
            return
        }
        getQQUtils()?.login()
    }

    private fun loginSuc() {
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        getQQUtils()?.onActivityResult(requestCode, resultCode, data)
        if (ILoginType.IActivityCode.RESULT_LOGIN_CODE == resultCode) {
            finish()
        }
    }

    private fun getQQUtils(): QQUtils? {
        if (qqUtils == null) {
            qqUtils = QQUtils(lifecycle)
        }
        return qqUtils
    }

    private fun getWxUtils(): WxUtils? {
        if (wxUtils == null) {
            wxUtils = WxUtils(this, lifecycle)
        }
        return wxUtils
    }

}